<?php

class NumberController extends Controller{

    public  $data = [];//请求存储

    public function beforeAction($action)
    {
        if(Yii::app()->request->isPostRequest){
            $this->data = $_POST ? $_POST : ['cityCode'=>'1101'] ;
            return true;
        }
    }

    /*
     * 测试
     */
    public function actionIndex()
    {
        echo '<pre>';
        $time = $this->_time();
        print_r($time);
    }

    /*
     * 时间判定
     */
    public function _time()
    {
        //查询时间
        $times = $this->data['time'] ? $this->data['time'] :'year';
        //当前时间
        $time = [];
        //日报
        if($times == 'day'){
            $time['time']['begin'] = mktime(0, 0, 0, date('m'), date('d')-1, date('Y'));
            $time['time']['end'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'))-1;
            for ( $i = 0 ; $i < 15; $i++ ){//前15天
                $time['statistics'][date('Y/m/d',mktime(0, 0, 0, date('m'), date('d')-$i-1, date('Y')))]['begin'] = mktime(0, 0, 0, date('m'), date('d')-$i-1, date('Y'));
                $time['statistics'][date('Y/m/d',mktime(0, 0, 0, date('m'), date('d')-$i-1, date('Y')))]['end'] = mktime(0, 0, 0, date('m'), date('d')-$i, date('Y'))-1;
            }
        }
        //周报
        if($times == 'week'){
            $time['time']['begin'] = mktime(0, 0, 0, date('m'), date('d')-date('w')+1-7, date('Y'));
            $time['time']['end'] = mktime(23, 59, 59, date('m'), date('d')-date('w')+7-7, date('Y'));
            for ( $i = 0 ; $i < 6; $i++ ){//前6周
                $key1 = date('Y/m/d',mktime(0, 0, 0, date('m'), date('d')-date('w')+1-7*($i+1), date('Y')));
                $key2 = date('Y/m/d',mktime(23, 59, 59, date('m'), date('d')-date('w')+7-7*($i+1), date('Y')));
                $time['statistics'][$key1.'-'.$key2]['begin'] = mktime(0, 0, 0, date('m'), date('d')-date('w')+1-7*($i+1), date('Y'));
                $time['statistics'][$key1.'-'.$key2]['end'] = mktime(23, 59, 59, date('m'), date('d')-date('w')+7-7*($i+1), date('Y'));
            }
        }
        //月报
        if($times == 'month'){
            $time['time']['begin'] = mktime(0, 0 , 0, date("m")-1, 1, date("Y"));
            $time['time']['end'] = mktime(23, 59, 59, date("m") ,0, date("Y"));
            for ( $i = 0 ; $i < 6; $i++ ) {//前6月
                $time['statistics'][date('m',mktime(0, 0 , 0, date("m")-($i+1), 1, date("Y")))]['begin'] = mktime(0, 0 , 0, date("m")-($i+1), 1, date("Y"));
                $time['statistics'][date('m',mktime(23, 59, 59, date("m")-$i , 0, date("Y")))]['end'] = mktime(23, 59, 59, date("m")-$i , 0, date("Y"));
            }
        }
        //季度报
        if($times == 'season'){
            $season = ceil((date('n'))/3)-1;//上季度是第几季度
            $time['time']['begin'] = mktime(0, 0, 0, $season*3-3+1, 1, date('Y'));
            $time['time']['end'] = mktime( 23, 59, 59, $season*3,date('t', mktime(0, 0 , 0, $season*3, 1, date("Y"))), date('Y'));
            for ( $i = 0 ; $i < 6; $i++ ) {//前6季度
                $time['statistics'][$i]['begin'] = mktime(0, 0, 0, $season*3-3*($i+1)+1, 1, date('Y'));
                $time['statistics'][$i]['end'] = mktime(23, 59, 59, $season*3-3*$i, date('t', mktime(0, 0 , 0, $season*3-3*$i, 1, date("Y"))),date('Y'));
            }
        }
        //年报
        if($times == 'year'){
            $time['time']['begin'] = mktime(0, 0, 0, 1, 1, date('Y'));
            $time['time']['end'] = time();
            for ( $i = 0 ; $i < 3; $i++ ) {//前3年
                $time['statistics'][date('Y')-($i)]['begin'] = mktime(0, 0, 0, 1, 1, date('Y')-($i));
                $time['statistics'][date('Y')-($i)]['end'] = mktime(23, 59, 59, 12, 31, date('Y')-($i));
                $time['statistics'][date('Y')]['begin'] = mktime(0, 0, 0, 1, 1, date('Y'));
                $time['statistics'][date('Y')]['end'] = time();
            }
        }
        //总报表
        for($i = (int)date('m'); $i >=1 ; $i--){ //当前年
            $time['now_total'][$i]['begin'] =  mktime(0, 0 , 0, $i, 1, date("Y"));
            $time['now_total'][$i]['end'] =  mktime(23, 59, 59, $i+1 , 0, date("Y"));
        }
        for($i = 12; $i >=1 ; $i--){ //上一年
            $time['sup_total'][$i]['begin'] =  mktime(0, 0 , 0, $i, 1, date("Y")-1);
            $time['sup_total'][$i]['end'] =  mktime(23, 59, 59, $i+1 , 0, date("Y")-1);
        }

        return $time;
    }

    /*
     * 数据接口
     */
    public function actionApi()
    {
        $model = [];
        $time = $this->_time();
        //推广
        $model['source'] = $this->_source($time['time']);
        //报名类型统计
        $model['type'] = $this->_type($time['time']);
        //报名统计
        $model['apply'] = $this->_apply($time['statistics']);
        //总报名
        $model['total']['now'] = $this->_apply($time['now_total']);
        $model['total']['sup'] = $this->_apply($time['sup_total']);
        echo '<pre>';
        print_r($model);
    }

    /*
     * 推广
     */
    public function _source($time){
        $areaId = $this->data['cityCode'];
        $begin = $time['begin'];
        $end = $time['end'];
        //推广占比
        $sqltrue = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND create_time BETWEEN $begin AND $end AND ( source_code != 0 OR source_code != '')";
        $data['true'] = Yii::app()->db->createCommand($sqltrue)->queryScalar();
        $sqlno = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND create_time BETWEEN $begin AND $end";
        $data['no'] = Yii::app()->db->createCommand($sqlno)->queryScalar()-$data['time']['true'];
        return $data;
    }

    /*
     * 报名类型统计
     */
    public function _type($time)
    {
        $areaId = $this->data['cityCode'];
        $begin = $time['begin'];
        $end = $time['end'];
        //看房报名
        $sql_line = "SELECT count(id) FROM ho_line_apply WHERE create_time BETWEEN $begin AND $end";
        $data['line'] = Yii::app()->db->createCommand($sql_line)->queryScalar();
        //专车看房
        $sql_car = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND types in (38,53,55) AND create_time BETWEEN $begin AND $end";
        $data['car'] = Yii::app()->db->createCommand($sql_car)->queryScalar();
        //地图资料
        $sql_map_book = "SELECT count(id) FROM ho_freemap WHERE areaId = $areaId AND type in (1,2) AND fadd_time BETWEEN $begin AND $end";
        $data['map_book'] = Yii::app()->db->createCommand($sql_map_book)->queryScalar();
        //楼盘报名  两张表
        $sql_house_apply = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND create_time 
                            BETWEEN $begin AND $end AND (house_id != '' OR house_id != 0 OR house_id != 1)";
        $sql_house_freemap = "SELECT count(id) FROM ho_freemap WHERE areaId = $areaId AND fadd_time 
                              BETWEEN $begin AND $end AND (house_id != '' OR house_id != 0 OR house_id != 1)";
        $data['house'] = Yii::app()->db->createCommand($sql_house_apply)->queryScalar()+Yii::app()->db->createCommand($sql_house_freemap)->queryScalar();
        //其他  两张表
        $sql_other_apply = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND create_time 
                            BETWEEN $begin AND $end AND types !=38 AND types !=53 AND types !=55 AND (house_id = '' OR house_id = 0 OR house_id = 1)";
        $sql_other_freemap = "SELECT count(id) FROM ho_freemap WHERE areaId = $areaId AND fadd_time 
                              BETWEEN $begin AND $end AND type != 1 AND type != 2 AND (house_id = '' OR house_id = 0 OR house_id = 1)";
        $data['other'] = Yii::app()->db->createCommand($sql_other_apply)->queryScalar()+Yii::app()->db->createCommand($sql_other_freemap)->queryScalar();
        return $data;
    }

    /*
     * 报名统计
     */
    public function _apply($time)
    {
        $areaId = $this->data['cityCode'];
        if(!empty($time)){
            foreach ($time as $k => $v) {
                $begin = $v['begin'];
                $end = $v['end'];
                //看房团报名
                $sql_line = "SELECT count(id) FROM ho_line_apply WHERE create_time BETWEEN $begin AND $end";
                $line = Yii::app()->db->createCommand($sql_line)->queryScalar();
                //apply 表报名
                $sql_apply = "SELECT count(id) FROM ho_apply WHERE areaId = $areaId AND create_time BETWEEN $begin AND $end";
                $apply = Yii::app()->db->createCommand($sql_apply)->queryScalar();
                //freemap 表报名
                $sql_map_book = "SELECT count(id) FROM ho_freemap WHERE areaId = $areaId AND fadd_time BETWEEN $begin AND $end";
                $freemap = Yii::app()->db->createCommand($sql_map_book)->queryScalar();
                $data[$k] = $line + $apply + $freemap;
            }
            return $data;
        }
    }
}
?>